package com.img.examine.controller;

import com.github.pagehelper.*;
import com.img.examine.entity.*;
import com.img.examine.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
public class IconController {
    @Autowired
    StudentService studentService;
    @Autowired
    EmployeeService employeeService;

    /**
     * 获取用户信息
     * @param page_num 页码
     * @param page_size 每页数量
     * @param category 类别，1为学生，2为职工
     * @param key_id 学号或职工号
     * @param name 姓名
     * @param update_time 更新登录时间
     * @return
     */

    @RequestMapping("/admin/img/examine/getImg")
    @ResponseBody
    public Map getImg(@RequestParam(defaultValue = "1") Integer page_num,@RequestParam(defaultValue = "20") Integer page_size,@RequestParam(defaultValue = "0")Integer category,
    String key_id, String name, Integer update_time){
        Map map = new HashMap();
        map.put("state",0);
        if(category==1){
            PageHelper.startPage(page_num,page_size);
            ArrayList<StudentEntity> studentEntities = studentService.getStudent(key_id, name, update_time);
            PageInfo<StudentEntity> pageInfo =new PageInfo<>(studentEntities);
            map.put("student",pageInfo);
        }else if(category==2){
            PageHelper.startPage(page_num,page_size);
            ArrayList<EmployeeEntity> employeeEntities = employeeService.getEmployee(key_id, name, update_time);
            PageInfo<EmployeeEntity> pageInfo =new PageInfo<>(employeeEntities);
            map.put("employee",pageInfo);
        }else{
            return map;
        }
        map.put("category",category);
        map.put("state",1);
        return map;
    }

    /**
     * 设置主页
     * @return
     */
    @RequestMapping("/admin/img/examine/index")
    public String index(){
        return "examine";
    }

    /**
     * 修改头像
     * @param KEY_ID 学号或职工号
     * @param category 身份：1为学生，2为职工
     * @return
     */
    @RequestMapping("/admin/img/examine/setImg")
    @ResponseBody
    public Map setImg(String KEY_ID,Integer category){
        Map map = new HashMap();
        map.put("state",0);
        Integer row = 0;
        if(category==null){
            return map;
        }else if (category==1){
            //学生
            if(studentService.setStudentIcon(KEY_ID)){
                row=1;
            }
        }else if(category==2){
            //职工
            if (employeeService.setEmployeeIcon(KEY_ID)){
                row=1;
            }
        }
        map.put("state",row);
        return map;
    }
}
